/**
 * 
 */
package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.menu.Button;
import org.alipay4j.model.menu.LabelRule;
import org.alipay4j.model.menu.Menu;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author heliang
 * 菜单操作组件
 */
public class MenuComponent extends AbstractComponent {

	public MenuComponent(Alipay alipay) {
		super(alipay);
	}
	
	/**
	* @Title: 个性化菜单创建 
	* @Description: 开发者可使用该接口创建个性化菜单，当生活号用户标签信息命中该菜单的标签规则时，用户将会看到该套菜单。个性化菜单创建完成后立即生效，用户支付宝客户端版本低于10.0.12时，若创建的是icon菜单，也会以文本菜单的形式展示。
	* @author heliang
	* @param buttons  一级菜单列表。
	* @param rules 标签规则
	* @param type 菜单类型
	* @return menu_key  该套个性化菜单key
	* @throws AlipayException 
	 */
	public String personalizedMenuCreate(List<Button> buttons,List<LabelRule> rules,String type) throws AlipayException {
	    if (ObjectHelper.isEmpty(rules)) {
            throw new IllegalArgumentException("rules can't be null or empty");
        }
	    Map<String, String> body = new HashMap<String, String>();
        body.put("button", JSONObject.toJSONString(buttons));
        body.put("label_rule", JSONObject.toJSONString(rules));
        body.put("type", type);
        body.put("method", "alipay.open.public.personalized.menu.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_personalized_menu_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_personalized_menu_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
	    return bodyRes.getString("menu_key");
	}
	
	
	/**
	* @Title: 个性化菜单删除 
	* @Description: 开发者可使用该接口删除个性化菜单
	* @author heliang
	* @param menu_key 要删除的个性化菜单key
	* @return boolean
	* @throws AlipayException 
	 */
	public boolean personalizedMenuDel(String menu_key) throws AlipayException {
		Map<String, String> body = new HashMap<String, String>();
		body.put("menu_key", menu_key);
		body.put("method", "alipay.open.public.personalized.menu.delete");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_personalized_menu_delete_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_personalized_menu_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
		return true;
	}
	
	
	/**
    * @Title: 菜单列表查询接口
    * @Description: 开发者可调用该接口查询自己创建的菜单列表
    * @author heliang
    * @return List<Menu>
    * @throws AlipayException 
     */
    public List<Menu> menuBatchquery() throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("method", "alipay.open.public.menu.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_menu_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_menu_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<Menu> menus = new ArrayList<Menu>();
        JSONArray array = bodyRes.getJSONArray("menus");
        for(int i=0;i<array.size();i++) {
            Menu menu = JSONObject.toJavaObject(array.getJSONArray(i), Menu.class);
            menus.add(menu);
        }
        return menus;
    }
	
	
    /**
    * @Title: 默认菜单创建接口 
    * @Description: 默认菜单创建接口
    * @author heliang
    * @param buttons  一级菜单列表
    * @param type 菜单类型
    * @return menu_key 默认菜单菜单key
    * @throws AlipayException 
     */
    public String menuCreate(List<Button> buttons,String type) throws AlipayException {
        if (ObjectHelper.isEmpty(buttons)) {
            throw new IllegalArgumentException("buttons can't be null or empty");
        }
        Map<String, String> body = new HashMap<String, String>();
        body.put("button", JSONObject.toJSONString(buttons));
        body.put("type", type);
        body.put("method", "alipay.open.public.menu.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_menu_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_menu_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("menu_key");
    }
    
	/**
	* @Title: 默认菜单更新接口 
	* @Description: 通过POST一个特定结构体，实现支付宝钱包客户端的公众账号更新自定义菜单。每一次的更新是针对全部自定义菜单的更新
	* @author heliang
	* @param buttons 一级菜单列表
	* @param type 菜单类型，支持值为icon：icon型菜单，text：文本型菜单，不传时默认为"text"，当传值为"icon"时，菜单节点的icon字段必传。
	* @return boolean 
	* @throws AlipayException 
	 */
	public boolean menuModify(List<Button> buttons,String type) throws AlipayException {
	    Map<String, String> body = new HashMap<String, String>();
        body.put("button", JSONObject.toJSONString(buttons));
        body.put("type", type);
        body.put("method", "alipay.open.public.menu.modify");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_menu_modify_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_menu_modify_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
	    return true;
	}

}
